package vote.ai.config;

import jakarta.annotation.PostConstruct;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import vote.ai.domain.enums.CorrectionModel;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * AI文本纠错配置类，从配置文件加载纠错模型相关配置
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "app.ai")
public class AiCorrectionConfig {

    private List<ModelConfig> models;
    private Map<CorrectionModel, ModelConfig> modelLookup = new HashMap<>();
    private Map<CorrectionModel, Integer> modelPriority = new HashMap<>();

    // 初始化模型映射
    @PostConstruct
    public void init() {
        if (models != null) {
            for (ModelConfig config : models) {
                modelLookup.put(config.getCode(), config);
                // 同时将模型名称和优先级存入映射
                if (config.getAiModel() != null && config.getOrder() > 0) {
                    modelPriority.put(config.getCode(), config.getOrder());
                }
            }
        }
    }

    public int getPriorityByCode(CorrectionModel code) {
        return modelPriority.getOrDefault(code, Integer.MAX_VALUE);
    }
    @Data
    public static class ModelConfig {
        private String name;
        private CorrectionModel code;
        private String appId;
        private String apiKey;
        private String apiSecret;
        private String host;
        private String urlPath;
        private boolean enabled = false;
        private int order;
        private String aiModel;
        private String promptPrefix;
        private String promptSuffix;
        private Map<String, Object> extraParams = new HashMap<>(); // 额外参数，用于灵活配置
    }
}
